import { login, setToken, getToken } from '@/utils/apis/user.js'

export default {
    state: {
        version: '1.0',
        loginInfo: '',
        token: getToken(),
    },
    getters: {
        ["user/_getVersion"](state) {
            return state.version ;
        }
    },
    mutations: {
        ['user/login'](state, data) {
            state.loginInfo = data;
        },
        ['user/setToken'] (state, token) {
            state.token = token
            setToken(token)
        },
    },
    actions: {
        ["user/handleLogin"]({state, dispatch, commit},{ userName, password }) {
            userName = userName.trim()
            return new Promise((resolve, reject) => {
                login({
                    phone: userName,
                    password,
                    type: 'admin',
                    loginType: 'RT'
                }).then(({ data }) => {
                    commit("user/login",data)
                    commit('user/setToken', data.subSessionId || '')
                    resolve()
                }).catch(err => {
                    reject(err)
                })
            })
        },
    }
};
